*******************************************************************************
* Combine all data sets
*******************************************************************************

* Oak Park

use "dataset_other_oak_park", clear
gen jurisdiction = "oak park, illinois"
gen design = "Cross-sectional"
replace pctyes = -pctyes + 1
save tmp, replace

* Other Louisiana parishes

use "dataset_other_louisiana_parishes", clear
gen jurisdiction = parish
append using tmp
save tmp, replace

* Other Texas cities

use "dataset_other_texas_cities", clear
gen jurisdiction = city
append using tmp
save tmp, replace

* EBR

use "dataset_east_baton_rouge_cross", replace
gen jurisdiction = "EBR"
encode precinct, gen(precinctc)
drop precinct
rename precinctc precinct
append using tmp
save tmp, replace

* FW

use "dataset_fort_worth_cross", replace
gen jurisdiction = "FW"
append using tmp
erase tmp.dta

*******************************************************************************
* Re-scale share Black within city
*******************************************************************************

replace pctblack = pctnhblack if mi(pctblack)
egen pctblackmin = min(pctblack), by(juris year)
egen pctblackmax = max(pctblack), by(juris year)
replace pctblack = (pctblack - pctblackmin) / (pctblackmax - pctblackmin)

*******************************************************************************
* Identify cities with post/pre data
*******************************************************************************

gen post = year >= 2020
egen postm = mean(post), by(juris)
gen postonly = postm == 1
gen preonly = postm == 0
gen panel = !inlist(postm, 0, 1)

*******************************************************************************
* Keep panel only
*******************************************************************************

drop if postm == 0 // (only pre)
di r(N_drop)
drop if postm == 1 // (only post)
di r(N_drop)

*******************************************************************************
* Number of elections since 2020
*******************************************************************************

bys year juris: gen rank = _n
egen postelectionm = rank(year) if rank == 1, by(juris) track
egen rankm = mean(postel), by(juris year)
egen rank2020 = max(rankm) if year < 2020, by(juris)
egen rank2020m = mean(rank2020), by(juris)
gen year2 = rankm - rank2020m

*******************************************************************************
* Pre/post indicators and interactions with race
*******************************************************************************

levelsof year2, local(levels)
foreach l of local levels {
	if `l' < 0 {
		local k = -`l'
		gen D_`k' = year2 == `l'
		gen D_`k'Xpctblack = D_`k'*pctblack
	}
	if `l' >= 0 {
		gen D`l' = year2 == `l'
		gen D`l'Xpctblack = D`l'*pctblack
	}
}
replace D_2 = 1 if D_3 == 1 | D_4 == 1 // pool if <=2 elections prior
replace D1 = 1 if D2 == 1

*******************************************************************************
* Juris-year dummies
*******************************************************************************

egen juris_year = group(juris year)

*******************************************************************************
* Regressions
*******************************************************************************

* these store the bootstrapped estimates
foreach x in B_2 B_1 B1 {
	gen `x' = .
}

eststo clear

foreach k in 1 2 {

	if `k' == 2 drop if inlist(jurisd, "FW", "EBR")
	
	eststo: xi: reg pctyes i.jurisd*pctblack ///
		D_2X D_1X D1X ///
		[aw=totalvotes], a(juris_year) cl(juris_year)
	estimates store reg`k'
	unique jurisdiction if e(sample)
	estadd scalar N_blocks = r(unique)
	if `k' == 1 estadd local ebrfw "Yes"
	if `k' == 2 estadd local ebrfw "No"
	
	* bootstrap
	forvalues i = 1(1)100 {
		di "doing bootstrap iteration `i'..."
		qui{
		preserve
			bsample, cluster(jurisd)
			xi: reg pctyes i.jurisd*pctblack ///
				D_2X D_1X D1X ///
				[aw=totalvotes], a(juris_year) cl(juris_year)
		restore
		replace B_2 = _b[D_2Xpctblack] if _n == `i'
		replace B_1 = _b[D_1Xpctblack] if _n == `i'
		replace B1 = _b[D1Xpctblack] if _n == `i'
		}
	}
	su B_2
	estadd local se_2 = "(" + string(r(sd), "%20.02fc") + ")" : reg`k'
	su B_1
	estadd local se_1 = "(" + string(r(sd), "%20.02fc") + ")" : reg`k'
	su B1
	estadd local se1 = "(" + string(r(sd), "%20.02fc") + ")" : reg`k'

}

*******************************************************************************
* Output table
*******************************************************************************

label variable D_2X "Share Black $\times 1\{s \leq -2\}$"
label variable D_1X "Share Black $\times 1\{s = -1\}$"
label variable D1X "Share Black $\times 1\{s \geq 1\}$"
			
#delimit;

esttab reg1 reg2
	using "_output/table A29.tex"
	,
		replace
		booktabs
		label
		nomtitles
		collabels(none)
		cells(b(fmt(3) star) se(par))
		width(\linewidth)
		keep(D*)
		stats(ebrfw N_clust N blank se_2 se_1 se1 N_blocks, 
			label(
				"Include EBR and FW"
				"Number of City-years"
				"Number of Observations"
				"Block bootstrapped standard errors:"
				"~~~Share Black $\times 1\{s \leq 2\}$"
				"~~~Share Black $\times 1\{s = -1\}$"
				"~~~Share Black $\times 1\{s \geq 1\}$"
				"~~~Number of Cities"
			)
			fmt(%20.0fc)
		)
		compress
		gap    	
		;
			
#delimit cr	

*******************************************************************************
* End
*******************************************************************************
